import axios from 'axios';
import type { AxiosRequestConfig } from 'axios';
import qs from 'query-string';

export interface PartnerIncome {
  id: number;
  partnerId: number;
  partnerName: string;
  type: number; // 1:分润 2:预存
  typeName?: string;
  sn?: string; // 订单号
  amount: number; // 金额
  status: number; // 0:待审核 1:已通过 2:已拒绝
  statusName?: string;
  remark?: string;
  operator?: string; // 操作人
  createTime: string;
  updateTime: string;
}

export interface PartnerIncomeQuery {
  current?: number;
  size?: number;
  partnerId?: number;
  incomeStatus?: number;
  startTime?: string;
  endTime?: string;
}

export interface PageResult<T> {
  records: T[];
  total: number;
  size: number;
  current: number;
  pages: number;
}

// 收入状态枚举
export enum IncomeStatusEnum {
  PENDING = 0, // 待审核
  APPROVED = 1, // 已通过
  REJECTED = 2, // 已拒绝
}

// 收入类型枚举
export enum IncomeTypeEnum {
  PROFIT = 1, // 分润
  DEPOSIT = 2, // 预存
}

// 分页查询预存记录
export function getPartnerIncomeList(params: PartnerIncomeQuery) {
  return axios.get<PageResult<PartnerIncome>>('/api/partner/income/page', {
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

// 统计预存金额
export function getPartnerIncomeSum(params: {
  partnerId?: number;
  startTime?: string;
  endTime?: string;
}) {
  return axios.get<number>('/api/partner/income/sum', {
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

// 新增预存金额
export function addPartnerDeposit(params: {
  partnerId: number;
  amount: number;
  remark?: string;
}) {
  return axios.post<boolean>('/api/partner/income/deposit', params, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    transformRequest: [
      function (data) {
        return qs.stringify(data);
      },
    ],
  } as AxiosRequestConfig);
}

// 减少预存金额（消费）
export function consumePartnerDeposit(params: {
  partnerId: number;
  amount: number;
  sn?: string;
  remark?: string;
}) {
  return axios.post<boolean>('/api/partner/income/consume', params, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    transformRequest: [
      function (data) {
        return qs.stringify(data);
      },
    ],
  } as AxiosRequestConfig);
}

// 审核预存单
export function auditPartnerIncome(params: {
  id: number;
  status: number;
}) {
  return axios.post<boolean>('/api/partner/income/audit', params, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    transformRequest: [
      function (data) {
        return qs.stringify(data);
      },
    ],
  } as AxiosRequestConfig);
} 